package com.jeeplus.cms.config.freemarker;

import com.jeeplus.cms.common.CmsProperties;
import com.jeeplus.common.utils.SpringContextHolder;
import freemarker.template.TemplateException;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * FreeMarker配置
 *
 * @author Sawyer、WuXilin
 */
@Configuration
public class FreeMarkerConfig {

    @Value("${server.servlet.context-path:}")
    protected String contextPath;

    @Autowired
    private CmsProperties cmsProperties;


    /*@Bean
    public DatabaseTemplateLoader databaseTemplateLoader(CmsTemplateService templateService) {
        return new DatabaseTemplateLoader(templateService);
    }*/

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(FreeMarkerProperties freeMarkerProperties) throws IOException, TemplateException {
        // 构建configurer
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPaths(freeMarkerProperties.getTemplateLoaderPath());
        configurer.setPreferFileSystemAccess(freeMarkerProperties.isPreferFileSystemAccess());
        configurer.setDefaultEncoding(freeMarkerProperties.getCharsetName());
        Properties settings = new Properties();
        settings.putAll(freeMarkerProperties.getSettings());
        configurer.setFreemarkerSettings(settings);

        // 加载自定义模板
        //configurer.setPostTemplateLoaders(databaseTemplateLoader);

        // 构建configuration
        freemarker.template.Configuration configuration = configurer.createConfiguration();
        this.configSharedVariable(configuration);
        configurer.setConfiguration(configuration);

        return configurer;
    }

    /**
     * 配置函数变量
     */
    private void configSharedVariable(freemarker.template.Configuration configuration) throws TemplateModelException {

        configuration.setSharedVariable("f", cmsProperties.getFrontPath());
        configuration.setSharedVariable("urlSuffix", cmsProperties.getUrlSuffix());
        configuration.setSharedVariable("ctxFrt", contextPath + cmsProperties.getFrontPath());
        configuration.setSharedVariable("ctxFrtStatic", contextPath + "/static");
        configuration.setSharedVariable("baiduAk", "baiduAk");


        // 配置函数方法
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        Map<String, TemplateMethodModelEx> templateMethodBeanMap = applicationContext.getBeansOfType(TemplateMethodModelEx.class, false, false);
        for (Map.Entry<String, TemplateMethodModelEx> item : templateMethodBeanMap.entrySet()) {
            configuration.setSharedVariable(item.getKey(), item.getValue());
        }
    }
}
